﻿#region (C) Header Region @ 2012
//====================================================
// 文件: LogXMLProvider.cs
// 描述: 
// 路径: Antu.Core\Logger\LogXMLProvider.cs
// ******************版本1 ******************
// 人员: 蔡金保    时间 : 2012/11/15 11:25:48
// 注释:
//====================================================
#endregion

using System;
using System.IO;
using System.Xml;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Globalization;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.Web;
using Antu.Core.Services;

namespace Antu.Core.Logger
{
    /// <summary>
    /// 将日志写入XML文件
    /// </summary>
    partial class LogXMLProvider : LogProviderBase
    {
        private string _logpath; //log的xml文件存放目录

        /// <summary>
        /// 创建日志文件
        /// </summary>
        /// <param name="logFile">日志文件</param>
        /// <param name="methodName">异常方法</param>
        /// <param name="parametersList">异常参数</param>
        /// <param name="exception">异常</param>
        /// <returns></returns>
        private bool CreateLog(string logFile, string methodName, string parametersList, Exception exception)
        {
            try
            {
                using (XmlTextWriter writer = new XmlTextWriter(logFile, System.Text.Encoding.UTF8))
                {
                    writer.Formatting = Formatting.Indented;
                    writer.Indentation = 4;
                    writer.WriteStartDocument(true);
                    writer.WriteStartElement("ErrorLog");
                    writer.WriteAttributeString("Date", String.Format("{0:yyyy-MM-dd}", DateTime.Today));
                    writer.WriteStartElement("ErrorTime");
                    writer.WriteAttributeString("Value", DateTime.Now.ToShortTimeString());
                    writer.WriteStartElement("Method");
                    writer.WriteValue(methodName);
                    writer.WriteEndElement();
                    writer.WriteStartElement("Parames");
                    writer.WriteValue(parametersList);
                    writer.WriteEndElement();
                    writer.WriteStartElement("ErrorMsg");
                    writer.WriteValue(exception.Message);
                    writer.WriteEndElement();
                    writer.WriteStartElement("Trace");
                    writer.WriteValue(exception.StackTrace);
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 追加到日志文件
        /// </summary>
        /// <param name="logFile">日志文件</param>
        /// <param name="methodName">异常方法</param>
        /// <param name="parametersList">异常参数</param>
        /// <param name="exception">异常</param>
        /// <returns></returns>
        private bool AppendLog(string logFile, string methodName, string parametersList, Exception exception)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                using (XmlTextReader reader = new XmlTextReader(logFile))
                {
                    doc.Load(reader);
                    XmlElement root = doc.DocumentElement;   //   获取根节点

                    XmlElement eErrorDate = doc.CreateElement("ErrorTime");
                    XmlElement eFuncName = doc.CreateElement("Method");
                    XmlElement eParames = doc.CreateElement("Parames");
                    XmlElement eMsg = doc.CreateElement("ErrorMsg");
                    XmlElement eTrace = doc.CreateElement("Trace");

                    eErrorDate.SetAttribute("Value", DateTime.Now.ToShortTimeString());
                    eFuncName.InnerText = methodName;
                    eParames.InnerText = parametersList;
                    eMsg.InnerText = exception.Message;
                    eTrace.InnerText = exception.StackTrace;

                    eErrorDate.AppendChild(eFuncName);
                    eErrorDate.AppendChild(eParames);
                    eErrorDate.AppendChild(eMsg);
                    eErrorDate.AppendChild(eTrace);
                    root.AppendChild(eErrorDate);
                }
                doc.Save(logFile);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private bool CreateLog(string logFile, string methodName, string parametersList, string msg)
        {
            try
            {
                using (XmlTextWriter writer = new XmlTextWriter(logFile, System.Text.Encoding.UTF8))
                {
                    writer.Formatting = Formatting.Indented;
                    writer.Indentation = 4;
                    writer.WriteStartDocument(true);
                    writer.WriteStartElement("InfoLog");
                    writer.WriteAttributeString("Date", String.Format("{0:yyyy-MM-dd}", DateTime.Today));
                    writer.WriteStartElement("InfoTime");
                    writer.WriteAttributeString("Value", DateTime.Now.ToShortTimeString());
                    writer.WriteStartElement("Method");
                    writer.WriteValue(methodName);
                    writer.WriteEndElement();
                    writer.WriteStartElement("Parames");
                    writer.WriteValue(parametersList);
                    writer.WriteEndElement();
                    writer.WriteStartElement("InfoMsg");
                    writer.WriteValue(msg);
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        private bool AppendLog(string logFile, string methodName, string parametersList, string msg)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                using (XmlTextReader reader = new XmlTextReader(logFile))
                {
                    doc.Load(reader);
                    XmlElement root = doc.DocumentElement;   //   获取根节点

                    XmlElement eInfoDate = doc.CreateElement("InfoTime");
                    XmlElement eFuncName = doc.CreateElement("Method");
                    XmlElement eParames = doc.CreateElement("Parames");
                    XmlElement eMsg = doc.CreateElement("InfoMsg");

                    eInfoDate.SetAttribute("Value", DateTime.Now.ToShortTimeString());
                    eFuncName.InnerText = methodName;
                    eParames.InnerText = parametersList;
                    eMsg.InnerText = msg;

                    eInfoDate.AppendChild(eFuncName);
                    eInfoDate.AppendChild(eParames);
                    eInfoDate.AppendChild(eMsg);
                    root.AppendChild(eInfoDate);
                }
                doc.Save(logFile);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override void Initialize(string name, NameValueCollection config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("没有相关配置!");
            }
            if (String.IsNullOrEmpty(name))
            {
                name = "LogXMLProvider";
            }
            if (String.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "创建XML日志提供者");
            }
            base.Initialize(name, config);

            if (config["logPath"] == null)
            {
                config["logPath"] = @"Log\";
            }
            _logpath = config["logPath"];
            config.Remove("logPath");

            //如果有多余的属性，则抛出异常
            if (config.Count > 0)
            {
                string attr = config.GetKey(0);
                if (!String.IsNullOrEmpty(attr))
                    throw new ProviderException("未指定的属性: " + attr);
            }
        }

        public override bool OutputErrorLog(string methodName, string parametersList, Exception exception)
        {
            string directoryname = HttpRuntime.AppDomainAppPath + _logpath;
            if (!Directory.Exists(directoryname))
                Directory.CreateDirectory(directoryname);
            string strFile = directoryname + String.Format("{0:yyyy-MM-dd}", DateTime.Today) + ".xml";

            if (!File.Exists(strFile))
                return CreateLog(strFile, methodName, parametersList, exception);
            else
                return AppendLog(strFile, methodName, parametersList, exception);
        }
        public override bool OutputInfoLog(string methodName, string parametersList, string msg,string fileext)
        {
            string directoryname = HttpRuntime.AppDomainAppPath + _logpath;
            if (!Directory.Exists(directoryname))
                Directory.CreateDirectory(directoryname);
            string strFile = directoryname + String.Format("{0:yyyy-MM-dd}", DateTime.Today) + "_" + fileext + ".xml";

            if (!File.Exists(strFile))
                return CreateLog(strFile, methodName, parametersList, msg);
            else
                return AppendLog(strFile, methodName, parametersList, msg);
        }
        public override bool OutputInfoLog(string methodName, string parametersList, string msg)
        {
            string directoryname = HttpRuntime.AppDomainAppPath + _logpath;
            if (!Directory.Exists(directoryname))
                Directory.CreateDirectory(directoryname);
            string strFile = directoryname + String.Format("{0:yyyy-MM-dd}", DateTime.Today) + "_info.xml";

            if (!File.Exists(strFile))
                return CreateLog(strFile, methodName, parametersList, msg);
            else
                return AppendLog(strFile, methodName, parametersList, msg);
        }
    }
}
